/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package paquetevistas.compra;

import Seguridad.Controlador.Validacion;
import controlador.compra.controladorcompra;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;
import modelos.Proveedor;
import modelos.Requerimientocompraxproducto;
import paquetevistas.Escritorio;

/**
 *
 * @author Jacklin
 */
public class NuevaCompra extends javax.swing.JInternalFrame {

    /**
     * Creates new form NuevaCompra
     */
    public static AsignarOrdenPedioaCompra compraorden;
    public static controladorcompra mycontroladorcompra = new controladorcompra();
    public static Proveedor proveedorcompra;
    public static List<Float> cantidades = new ArrayList<Float>();//usado para recibir los datos de la ventana asignar orden de pedido a compra
    public static List<Requerimientocompraxproducto> reqscompxprod = new ArrayList<Requerimientocompraxproducto>();//usado para recibir los datos de la ventana asignar orden de pedido a compra
    public static Validacion validacion = new Validacion();

    public NuevaCompra() {
        initComponents();
        loadComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jprecioAcordado = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jComboProveedor = new javax.swing.JComboBox();
        jfechaAcordada = new com.toedter.calendar.JDateChooser();
        jLayeredPane2 = new javax.swing.JLayeredPane();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setTitle("Compra Nueva");

        jLabel2.setText("Fecha Acordada:");
        jLayeredPane1.add(jLabel2);
        jLabel2.setBounds(20, 30, 140, 10);

        jLabel3.setText("Precio Acordado (S/.):");
        jLayeredPane1.add(jLabel3);
        jLabel3.setBounds(20, 60, 140, 14);

        jprecioAcordado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jprecioAcordadoActionPerformed(evt);
            }
        });
        jLayeredPane1.add(jprecioAcordado);
        jprecioAcordado.setBounds(190, 60, 180, 20);

        jLabel1.setText("Proveedor");
        jLayeredPane1.add(jLabel1);
        jLabel1.setBounds(20, 90, 110, 30);

        jComboProveedor.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Eliga un proveedor" }));
        jLayeredPane1.add(jComboProveedor);
        jComboProveedor.setBounds(190, 90, 180, 30);
        jLayeredPane1.add(jfechaAcordada);
        jfechaAcordada.setBounds(190, 20, 180, 20);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/save.png"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jLayeredPane2.add(jButton1);
        jButton1.setBounds(30, 20, 65, 41);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/plus.png"))); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jLayeredPane2.add(jButton3);
        jButton3.setBounds(160, 20, 60, 40);

        jLabel9.setText("Guardar");
        jLayeredPane2.add(jLabel9);
        jLabel9.setBounds(40, 70, 60, 14);

        jLabel10.setText("Asignar Orden de Pedido");
        jLayeredPane2.add(jLabel10);
        jLabel10.setBounds(130, 60, 170, 30);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/limpiar.png"))); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jLayeredPane2.add(jButton2);
        jButton2.setBounds(280, 20, 59, 40);

        jLabel11.setText("Limpiar");
        jLayeredPane2.add(jLabel11);
        jLabel11.setBounds(290, 70, 70, 14);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLayeredPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 383, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 392, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(49, 49, 49))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLayeredPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        int row = jComboProveedor.getSelectedIndex();
        if (row == 0) {
            JOptionPane.showMessageDialog(null, "Debe elegir un proveedor");
        } else {
            if (compraorden == null || compraorden.isClosed()) {
                String razonprov = jComboProveedor.getSelectedItem().toString();
                proveedorcompra = mycontroladorcompra.sacaproveedorbyrazon(razonprov);
                compraorden = new AsignarOrdenPedioaCompra(jprecioAcordado);
                Escritorio.compra.getDesktopPane().add(compraorden);
                compraorden.setVisible(true);
            } else {
                try {
                    compraorden.setSelected(true);
                } catch (Exception e) {
                }
            }
        }

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        jfechaAcordada.setDate(null);
        jprecioAcordado.setText("");
        jComboProveedor.setSelectedIndex(0);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        Integer row = jComboProveedor.getSelectedIndex();
        if (validacion.es_vacio(jprecioAcordado.getText())) {
            JOptionPane.showMessageDialog(this, validacion.msj + "cantidad a cubrir la compra");
        } else if (!validacion.es_numero_real(jprecioAcordado.getText())) {
            JOptionPane.showMessageDialog(this, validacion.msj);
        } else {
            Float numcantidad = Float.parseFloat(jprecioAcordado.getText());
            if (jprecioAcordado.getText().equalsIgnoreCase("0") || (numcantidad < 0)) {
                JOptionPane.showMessageDialog(this, "Debe agregar una cantidad mayor a 0 del producto a comprar");
            } else 
            if ((!jprecioAcordado.getText().isEmpty() && !jprecioAcordado.getText().equalsIgnoreCase("0") && numcantidad > 0) && row.compareTo(0) != 0) {
                if((!cantidades.isEmpty())&&(!reqscompxprod.isEmpty())){
                    Float precioAcordado = Float.parseFloat(jprecioAcordado.getText());
                Date fechaAcordada = jfechaAcordada.getDate();
                String proveedor = jComboProveedor.getSelectedItem().toString();
                boolean estado=mycontroladorcompra.guardacompra(precioAcordado, fechaAcordada, proveedor);
                if (estado==true)mycontroladorcompra.guardadetallecompra(cantidades, reqscompxprod);
                lbj.LBJ.Login.escritorio.compra.refreshPurchaseTable();
                this.dispose();
                }
            }
        }

        if (row.compareTo(0) == 0) {
            JOptionPane.showMessageDialog(this, "Debe seleccionar una proveedor");
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jprecioAcordadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jprecioAcordadoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jprecioAcordadoActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboProveedor;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLayeredPane jLayeredPane2;
    private com.toedter.calendar.JDateChooser jfechaAcordada;
    private javax.swing.JTextField jprecioAcordado;
    // End of variables declaration//GEN-END:variables

    void loadComponents() {
//        jComboProveedor.removeAllItems();
        List<Proveedor> proveedores = new ArrayList<Proveedor>();
        proveedores = mycontroladorcompra.sacaproveedores();
        for (int i = 0; i < proveedores.size(); i++) {
            jComboProveedor.addItem(proveedores.get(i).getRazonsocial());
        }
        jprecioAcordado.setText("" + 0.0);
        jComboProveedor.setSelectedIndex(0);
    }
}
